<template>
  <div>
    <el-row>
      <el-table
          fixed
          :data="tableData"
          style="width: 100%">
        <el-table-column
            fixed
            type="index"
            label="序号"
            width="140">
        </el-table-column>
        <el-table-column
            prop="username"
            label="用户名"
            width="360">
        </el-table-column>
        <el-table-column
            prop="pwMd5"
            label="密码"
            width="400">
        </el-table-column>

        <el-table-column
            prop="name"
            label="昵称"
            width="360">
        </el-table-column>

        <el-table-column
            prop="status"
            label="状态"
            width="150">
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="start(scope.$index)">启用</el-button>
            <el-button
                size="mini"
                type="warning"
                @click="ban(scope.$index)">禁用</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="del(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>

  </div>
</template>

<script>
export default {
  name: "User",
  data() {
    return {
      tableData: [],
      bookFormVisible:false,
      addFormVisible:false,
      formLabelWidth:'120px',
    }
  },
  methods:{
    getAllUser() {
      this.$axios.get("/user/get").then(res => {
        if(res.data.code == 0) {
          this.tableData = res.data.data
          this.$message.success("获取成功")
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    start(index) {
      if(this.tableData[index].status == 1) {
        this.$message.error("无效操作")
      } else {
        let user = this.tableData[index]
        user["status"] = 1
        this.$axios.post("/user/updateUser",user).then(res => {
          if(res.data.code == 0) {
            this.$message.success("停用成功")
          } else {
            this.$message.error(res.data.message)
          }
        })
      }
      location.reload()
    },
    ban(index) {
      if(this.tableData[index].status == 0) {
        this.$message.error("无效操作")
      } else {
        let user = this.tableData[index]
        user["status"] = 0
        this.$axios.post("/user/updateUser",user).then(res => {
          if(res.data.code == 0) {
            this.$message.success("停用成功")
          } else {
            this.$message.error(res.data.message)
          }
        })
      }
      location.reload()
    },
    del(index) {
      this.$confirm('确认删除该用户吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.post("/user/delUser",this.tableData[index]).then(res => {
          if(res.data.code == 0) {
            this.$message.success("删除成功")
            location.reload()
          } else {
            this.$message.error(res.data.message)
          }
        })
      }).catch();

    }
  },
  created() {
    this.getAllUser()
  }
}
</script>

<style scoped>

</style>